package com.thoughtworks.bytecode2class;

import com.thoughtworks.bytecode2class.attribute.Attribute;
import com.thoughtworks.bytecode2class.constantpool.Constant;

public class ClassInspector {

    private final ClassReader classReader;
    private final ClassVisitor visitor;

    public ClassInspector(ClassReader classReader) {
        this(classReader, new DefaultClassVisitor());
    }

    public ClassInspector(ClassReader classReader, ClassVisitor acceptor) {
        this.classReader = classReader;
        this.visitor = acceptor;
    }

    public void inspect() {
        constants();
        fields();
        methods();
        attributes();
    }

    public void constants() {
        visitor.beginConstants();
        for (Constant constant : classReader.getConstantPool().getConstants()) {
            constant.description();
        }
    }

    public void fields() {
        visitor.beginFields();
        for (MethodAndFieldRef field : classReader.getFields()) {
            visitor.acceptMedhodAndFieldRef(field);
        }
    }

    public void methods() {
        visitor.beginMethods();
        for (MethodAndFieldRef method : classReader.getMethods()) {
            visitor.visitMethod(method);
        }
    }

    public void attributes() {
        visitor.beginAttributes();
        for (Attribute attribute : classReader.getAttributes()) {
            visitor.visitAttribute(attribute);
        }
    }

}
